<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetFlashSale.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\common;

use app\api\model\dao\FlashSale;
use app\api\model\dao\Goods;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class GetFlashSale extends PageBase
{

    private $objFlashSale;
    private $objGoods;

    public function __construct()
    {
        parent::__construct();
        $this->objFlashSale = new FlashSale();
        $this->objGoods = new Goods();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'user_id' => 'require',
        ];
        $msg = [
            'user_id.require' => '用户ID必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {

        // TODO:业务逻辑
        // 1、参数校验
        //$ret = $this->checkParam($param);
        //if ($ret) {
        //    return $this->getRes(Error::ERR_PARAM, $ret);
        //}

        $perpage = isset($param['perpage']) ? intval($param['perpage']) : 0;
        $page = isset($param['page']) ? intval($param['page']) : 0;
        $perpage = max(20, $perpage);
        $page = max(1, $page);
        $offset = ($page - 1) * $perpage;
        $limit = $perpage;

        $flashSalesArr = [];
        $dates = [];
        $now = time();
        $dates[] = $now;
        $dates[] = $now + (24 * 60 * 60);

        foreach ($dates as $date) {
            $start = strtotime(date('Y-m-d', $date));
            $end = $start + (24 * 60 * 60);
            $arrConds = [
                'is_end' => ['=', 0],
                'start_time' => ['<=', $end],
                'end_time' => ['>=', $start],
            ];
            $arrFields = ['goods_id', 'start_time', 'end_time', 'is_end'];

            $flashSales = $this->objFlashSale->geFlashSaleByCond($arrConds, $arrFields);
            $goods_arr = array();
            if (!empty($flashSales)) {
                $goods_ids = array_map(function ($v) {
                    return $v['goods_id'];
                }, $flashSales);
                $goods = $this->objGoods->getGoods(implode(',', $goods_ids));
                foreach ($goods as $good) {
                    $goods_arr[$good['goods_id']] = $good;
                }
                foreach ($flashSales as &$flashSale) {
                    if (isset($goods_arr[$flashSale['goods_id']])) {
                        $flashSale['start_time'] = date('Y-m-d H:m:s', $flashSale['start_time']);
                        $flashSale['end_time'] = date('Y-m-d H:m:s', $flashSale['end_time']);
                        $flashSale['goods_name'] = $goods_arr[$flashSale['goods_id']]['goods_name'];
                        $flashSale['market_price'] = $goods_arr[$flashSale['goods_id']]['market_price'];
                        $flashSale['shop_price'] = $goods_arr[$flashSale['goods_id']]['shop_price'];
                        $flashSale['goods_number'] = $goods_arr[$flashSale['goods_id']]['goods_number'];
                        $flashSale['sale_count'] = $goods_arr[$flashSale['goods_id']]['sale_count'];
                        $flashSale['sale_percent'] = '' . round($flashSale['sale_count'] / $flashSale['goods_number'], 2) * 100 . '%';
                        // 加上折扣
                        $flashSale['discount'] = '' . round($goods_arr[$flashSale['goods_id']]['shop_price'] / $goods_arr[$flashSale['goods_id']]['market_price'], 2) * 10;
                        $flashSale['goods_thumb'] = $goods_arr[$flashSale['goods_id']]['goods_thumb'];
                    }
                }
                $flashSalesArr[] = [
                    'list' => $flashSales,
                    'now' => date('Y-m-d H:m:s', $start),
                    'end' => date('Y-m-d H:m:s', $end),
                ];
            }
        }

        $this->data = $flashSalesArr;

        return $this->getRes();
    }
}
